package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.salelogistics.basic.utils.CheckParameterUtil;
import com.salelogistics.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.domain.BasMaterialBrand;
import com.salelogistics.basic.service.IBasMaterialBrandService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 材料品牌Controller
 *
 * @author ruoyi
 * @date 2023-07-20
 */
@RestController
@RequestMapping("/brand")
public class BasMaterialBrandController extends BaseController {
    @Autowired
    private IBasMaterialBrandService basMaterialBrandService;

    //数据校验工具
    CheckParameterUtil checkParameterUtil = new CheckParameterUtil();

    //随机数
    Random random = new Random();

    /**
     * 查询材料品牌列表
     */
    @RequiresPermissions("basic:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(BasMaterialBrand basMaterialBrand) {
        startPage();
        List<BasMaterialBrand> list = basMaterialBrandService.selectBasMaterialBrandList(basMaterialBrand);
        return getDataTable(list);
    }

    /**
     * 导出材料品牌列表
     */
    @RequiresPermissions("basic:brand:export")
    @Log(title = "材料品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasMaterialBrand basMaterialBrand) {
        List<BasMaterialBrand> list = basMaterialBrandService.selectBasMaterialBrandList(basMaterialBrand);
        ExcelUtil<BasMaterialBrand> util = new ExcelUtil<BasMaterialBrand>(BasMaterialBrand.class);
        util.exportExcel(response, list, "材料品牌数据");
    }

    /**
     * 获取材料品牌详细信息
     */
    @RequiresPermissions("basic:brand:query")
    @GetMapping(value = "/{materialBrandId}")
    public AjaxResult getInfo(@PathVariable("materialBrandId") Long materialBrandId) {
        return success(basMaterialBrandService.selectBasMaterialBrandByMaterialBrandId(materialBrandId));
    }

    /**
     * 新增材料品牌
     */
    @RequiresPermissions("basic:brand:add")
    @Log(title = "材料品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasMaterialBrand basMaterialBrand) {
        if (checkParameterUtil.checkParameter(basMaterialBrand, "basMaterialBrand")) {
            int randomNumber = random.nextInt(900000) + 100000;
            basMaterialBrand.setMaterialBrandCode("PI" + randomNumber);
            basMaterialBrand.setCreateBy(SecurityUtils.getUsername());
            basMaterialBrand.setEditedBy(SecurityUtils.getUsername());
            System.out.println(basMaterialBrand);
            return toAjax(basMaterialBrandService.insertBasMaterialBrand(basMaterialBrand));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 修改材料品牌
     */
    @RequiresPermissions("basic:brand:edit")
    @Log(title = "材料品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasMaterialBrand basMaterialBrand) {
        if (checkParameterUtil.checkParameter(basMaterialBrand, "basMaterialBrand")) {

            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            basMaterialBrand.setEditedBy(SecurityUtils.getUsername());
            basMaterialBrand.setEditedTime(currentTime);
            return toAjax(basMaterialBrandService.updateBasMaterialBrand(basMaterialBrand));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 删除材料品牌
     */
    @RequiresPermissions("basic:brand:remove")
    @Log(title = "材料品牌", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialBrandIds}")
    public AjaxResult remove(@PathVariable Long[] materialBrandIds) {
        return toAjax(basMaterialBrandService.deleteBasMaterialBrandByMaterialBrandIds(materialBrandIds));
    }

    /**
     * 修改品牌状态
     */
    @RequiresPermissions("basic:brand:edit")
    @Log(title = "材料品牌", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeBrandStatus(@RequestBody BasMaterialBrand basMaterialBrand) {
        if (checkParameterUtil.checkParameter(basMaterialBrand, "basMaterialBrand")) {
            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            basMaterialBrand.setEditedBy(SecurityUtils.getUsername());
            basMaterialBrand.setEditedTime(currentTime);
            return toAjax(basMaterialBrandService.changeBrandStatus(basMaterialBrand));
        } else {
            return error("参数异常");
        }

    }
}
